/*
 *  NFCSigning - Open source library for signing/validation of NDEF messages
 *  Copyright (C) 2009-2010 The NFCSigning Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */
package org.nfcsigning.record;

/**
 *
 * @author Markus Kilås
 */
class Arrays {
       
    public static boolean equals(byte[] bytes1, byte[] bytes2) {
        if(bytes1 == null && bytes2 == null) {
            return true;
        } else if(bytes1 == null || bytes2 == null) {
            return false;
        }
        
        if(bytes1.length != bytes2.length) {
            return false;
        }
        
        for(int i = 0; i < bytes1.length; i++) {
            if(bytes1[i] != bytes2[i]) {
                return false;
            }
        }
        
        return true;
    }
    
    public static int hashCode(byte bytes[]) {
        
        if (bytes == null) {
            return 0;
        }
        
        int result = 1;
        
        for(int i = 0; i < bytes.length; i++) {
            result = 31 * result + bytes[i];
        }

        return result;
    }

    public static boolean equals(Object[] o1, Object[] o2) {
        if(o1 == null && o2 == null) {
            return true;
        } else if(o1 == null || o2 == null) {
            return false;
        }
        
        if(o1.length != o2.length) {
            return false;
        }
        
        for(int i = 0; i < o1.length; i++) {
            if(o1[i] != o2[i]) {
                return false;
            }
        }
        
        return true;
    }
    
    public static int hashCode(Object[] bytes) {
        
        if (bytes == null) {
            return 0;
        }
        
        int result = 1;
        
        for(int i = 0; i < bytes.length; i++) {
            result = 31 * result + (bytes[i] == null ? 0 : bytes[i].hashCode());
        }

        return result;
    }
}
